$(function () {
    $("#jqGrid").jqGrid({
        url: '../recruitdamand/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden: true},
			{ label: '用工单位', name: 'settlementno', index: 'settlementno',hidden: true },
            { label: '用工单位名称', name: 'settlementname', index: 'settlementname' },
            { label: '需求编号', name: 'damandid', index: 'damandid',hidden: true },
			{ label: '岗位编号', name: 'jobid', index: 'jobid',hidden: true },
			{ label: '岗位名称', name: 'jobname', index: 'jobname' },
            { label: '需求人数', name: 'empnum', index: 'empnum' },
            { label: '完成招聘日期', name: 'finishdate', index: 'finishdate',formatter:"date", formatoptions: {newformat:'Y-m-d'} },
			{ label: '性别', name: 'sex', index: 'sex',formatter:function(v){
                if(v=="0"){
                    return "男";
                }else if(v=="1"){
                    return "女";
                }else{
                    return ""
                }
            } },
			{ label: '最大年龄', name: 'topage', index: 'topage' }, 			
			{ label: '最小年龄', name: 'lowage', index: 'lowage' }, 			
			{ label: '学历', name: 'education', index: 'education' }, 			
			{ label: '婚否', name: 'married', index: 'married' }, 			
			{ label: '证书名称', name: 'certificatename', index: 'certificatename' },
            { label: '面试参加人数', name: 'joinfircount', index: 'join_fir_count',width: 100},
            { label: '面试合格人数', name: 'fircount', index: 'fir_count',width: 100 },
            { label: '复试参加人数', name: 'joinseccount', index: 'join_sec_count',width: 100 },
            { label: '复试合格人数', name: 'seccount', index: 'sec_count' ,width: 100},
            { label: '体检参加人数', name: 'joinphysicalcount', index: 'join_physical_count' ,width: 100},
            { label: '体检合格人数', name: 'physicalcount', index: 'physical_count' ,width: 100}
        ],
		viewrecords: true,
        height: $(window).height()*0.8,
        rowNum: 30,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        //字段太多自动加载横轴滚动条
        shrinkToFit:false,
        autoScroll: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
            //doResize();
            //显示grid底部滚动条
            //$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "scroll" });
        }
    });
    var r =utils.getDictList("post,sex,education");
    vm.post = r.post;
    vm.sex = r.sex;
    vm.education = r.education;
});

//自动跳转列表宽度
/*function doResize(){
    var td=$('#tdCompute')//获取计算实际列长度的容器
        ,tds//临时保存列
        ,arr=[];//用于保存最大的列宽
    //遍历每行获得每行中的最大列宽
    $('#gview_jqGrid .ui-jqgrid-htable tr,#jqGrid tr:gt(0)').each(function(){
        $(this).find('td,th').each(function(idx){
            arr[idx]=Math.max(arr[idx]?arr[idx]:0,td.html($(this).text())[0].offsetWidth);
        })
    });
    $('#gview_jqGrid th').each(function(idx){this.style.width=arr[idx]+'px'});//设置页头单元格宽度
    $('#jqGrid tr:eq(0) td').each(function(idx){this.style.width=arr[idx]+'px'});//设置内容表格中控制单元格宽度的单元格，在第一行

    var total=$('#jqGrid').closest('.ui-jqgrid-bdiv')[0].scrollWidth;//获取表格宽度
    $('#jqGrid,#gview_jqGrid .ui-jqgrid-htable').css({width:total});//设置表头表格和内容表格宽度
    $('#gbox_jqGrid,#gview_jqGrid,#gview_jqGrid>div:gt(0)').css({width:total+15});//设置表头表格和内容表格父容器的宽度
}*/

var vm = new Vue({
	el:'#rrapp',
	data:{
        q: {
            settlementname: null
        },
		showList: true,
		showTable:true,
		title: null,
		recruitdamand: {},
		list: {},
        post:{},
        sex:{},
        education:{}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.recruitdamand = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.recruitdamand.id == null ? "../recruitdamand/save" : "../recruitdamand/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.recruitdamand),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}

			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../recruitdamand/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		choose: function(){
            var ids = getSelectedRows();
            if(ids==null){
                return ;
            }
            if(ids.length!=1){
                alert("只能选择一行记录！");
                return ;
            }
            vm.showTable = false;
            $("#jqGridTable").jqGrid({
                url: '../recruitdamand/choose',
                datatype: "json",
                postData: {"ids":JSON.stringify(ids)},
                colModel: [
                    { label: 'id', name: 'id', index: 'id', key: true, hidden: true },
                    { label: '求职单位', name: 'settlementno', index: 'settlementno' },
                    { label: '用工单位名称', name: 'settlementname', index: 'settlementname' },
                    { label: '需求编号', name: 'damandid', index: 'damandid' },
                    // { label: '岗位编号', name: 'jobid', index: 'jobid' },
                    { label: '岗位名称', name: 'jobname', index: 'jobname' },
                    { label: '姓名', name: 'name', index: 'name' },
                    { label: '性别', name: 'sex', index: 'sex' },
                    { label: '年龄', name: 'age', index: 'topage' },
                    { label: '学历', name: 'education', index: 'education' },
                    { label: '婚否', name: 'married', index: 'married' },
                    { label: '证书名称', name: 'papername', index: 'papername' }
                ],
                viewrecords: true,
                height: 385,
                rowNum: 100,
                //rowList : [10,30,50],
                rownumbers: true,
                //rownumWidth: 25,
                autowidth:true,
                multiselect: true,
                pager: "#jqGridPagerTable",
                jsonReader : {
                    root: "page.list",
                    page: "page.currPage",
                    total: "page.totalPage",
                    records: "page.totalCount"
                },
                /*prmNames : {
                    page:"page",
                    rows:"limit",
                    order: "order"
                },*/
                gridComplete:function(){
                    //隐藏grid底部滚动条
                    $("#jqGridTable").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
                }
            });

            var page = $("#jqGridTable").jqGrid('getGridParam','page');
            $("#jqGridTable").jqGrid('setGridParam',{
                page:page,
                postData: {"id":JSON.stringify(ids)}
            }).trigger("reloadGrid");

            /*var ids = getSelectedRows();
            if(ids==null){
                return ;
            }
            $.ajax({
                type: "POST",
                url: "../recruitdamand/choose",
                contentType: "application/json",
                data: JSON.stringify(ids),
                success: function(r){
                     vm.showTable = false;
                     vm.list = r.list;
                }
            });*/
		},
		selected: function(){
            var rowDatas = vm.getRows();
            if(!rowDatas){
                return ;
            }
            var id = getSelectedRow();
            if(id==null){
                return ;
            }
            $.ajax({
                type: "POST",
                url: "../recruitdamand/selected?id="+id,
                contentType: "application/json",
                data: JSON.stringify(rowDatas),
                success: function(r){
                    if(r.code == 0){
                        alert('操作成功', function(index){
                           $("#jqGrid").trigger("reloadGrid");
                            $("#jqGridTable").trigger("reloadGrid");
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });

		},
		getInfo: function(id){
			$.get("../recruitdamand/info/"+id, function(r){
                vm.recruitdamand = r.recruitdamand;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    'settlementname': vm.q.settlementname,
                    'jobname': vm.q.jobname,'sex': vm.q.sex,
                    'education': vm.q.education},
                page:page
            }).trigger("reloadGrid");
		},
		getRows: function(){
            //选择多条记录
			var grid = $("#jqGridTable");
			var rowKey = grid.getGridParam("selrow");
			if(!rowKey){
				alert("请选择一条记录");
				return ;
			}

			var ids = grid.getGridParam("selarrrow");
            var rowDatas = new Array();
			ids.forEach(function(val,index){
                rowDatas[index] = $("#jqGridTable").jqGrid("getRowData",val);
			});
			return rowDatas;

		}
	}
});